package com.education.myproject.ui;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.education.myproject.R;
import com.education.myproject.base.BaseActivity;
import com.education.myproject.ui.main.MainActivity;

import org.jokar.permissiondispatcher.annotation.NeedsPermission;
import org.jokar.permissiondispatcher.annotation.OnNeverAskAgain;
import org.jokar.permissiondispatcher.annotation.OnPermissionDenied;
import org.jokar.permissiondispatcher.annotation.OnShowRationale;
import org.jokar.permissiondispatcher.annotation.RuntimePermissions;
import org.jokar.permissiondispatcher.library.PermissionRequest;

@RuntimePermissions
public class SplashActivity extends BaseActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SplashActivityPermissionsDispatcher.readSdcardWithCheck(this);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @NeedsPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
    void readSdcard() {
        //授权通过后
        Toast.makeText(this, "可以读取sdcard", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        SplashActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @OnShowRationale(Manifest.permission.READ_EXTERNAL_STORAGE)
    void showRationale(final PermissionRequest request) {
        //显示申请的原因
        new AlertDialog.Builder(SplashActivity.this)
                .setTitle("我要权限")
                .setMessage("该权限可以在sdcard中写入缓存")
                .setNegativeButton("允许", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.proceed();
                    }
                }).setPositiveButton("请手动打开", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivity(intent);
            }
        })
                .show();
    }

    @OnPermissionDenied(Manifest.permission.READ_EXTERNAL_STORAGE)
    void permissionDenied() {
        Toast.makeText(this, "权限被拒绝", Toast.LENGTH_SHORT).show();
        SplashActivityPermissionsDispatcher.readSdcardWithCheck(this);
    }

    @OnNeverAskAgain(Manifest.permission.READ_EXTERNAL_STORAGE)
    void neverAsk() {
        Toast.makeText(this, "权限不再提示", Toast.LENGTH_SHORT).show();
        new AlertDialog.Builder(SplashActivity.this)
                .setTitle("我要权限")
                .setMessage("该权限可以在sdcard中写入缓存")
                .setPositiveButton("请手动打开", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivity(intent);
            }
        })
                .show();
    }
}
